﻿using System;
using System.Web.UI;
using Presentation;
using Infrastructure;


namespace WebClient
{
    public partial class WebFormHuffmanCode : Page, IView
    {
        private Presenter presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Session["presenter"] == null)
            {
                var logger = new TxtLogger("log_" + Session.SessionID + ".txt");
                presenter = new Presenter(this, logger);
                Session["logger"] = logger;
                Session["presenter"] = presenter;
            }
            else
            {
                presenter = (Presenter)Session["presenter"];
                presenter.setView(this);
            }
        }

        public string Log
        {
            get
            {
                return (string)Session["log"];
            }
            set
            {
                Session["log"] = value;
            }
        }
        public string CharToCode
        {
            get
            {
                return (string)Session["CharToCode"];
            }
            set
            {
                Session["CharToCode"] = value;
            }
        }
        public string CodeToChar
        {
            get
            {
                return (string)Session["CodeToChar"];
            }
            set
            {
                Session["CodeToChar"] = value;
            }
        }
        public string CharacterString
        {
            get
            {
                return (string)Session["CharacterString"];
            }
            set
            {
                Session["CharacterString"] = value;
            }
        }
        public string HuffmanCode
        {
            get
            {
                return (string)Session["HuffmanCode"];
            }
            set
            {
                Session["HuffmanCode"] = value;
            }
        }
        public string Status
        {
            get
            {
                return (string)Session["Status"];
            }
            set
            {
                Session["Status"] = value;
            }
        }

        public event Action OnEncode;
        public event Action OnDecode;
        public event Action OnStringChanged;
        public event Action OnCodeChanged;
        public event Action OnBuildTree;

        protected void ButtonEncode_Click(object sender, EventArgs e)
        {
            CharacterString = TextBoxString.Text;
            OnEncode();
            TextBoxCode.Text = HuffmanCode;
            TextBoxCodeToChar.Text = CodeToChar;
            TextBoxCharToCode.Text = CharToCode;
            LabelStatus.Text = Status;
            TextBoxLog.Text = Log;
        }

        protected void ButtonDecode_Click(object sender, EventArgs e)
        {
            HuffmanCode = TextBoxCode.Text;
            OnDecode();
            TextBoxString.Text = CharacterString;
            LabelStatus.Text = Status;
            TextBoxLog.Text = Log;
        }

        protected void RadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (RadioButtonList.SelectedIndex)
            {
                case 0:
                    presenter.setLogger(new TxtLogger());
                    break;
                case 1:
                    presenter.setLogger(new HtmlLogger());
                    break;
            }
            TextBoxLog.Text = Log;
        }

        protected void TextBoxString_TextChanged(object sender, EventArgs e)
        {
            OnStringChanged();
        }

        protected void TextBoxCode_TextChanged(object sender, EventArgs e)
        {
            OnCodeChanged();
        }

        protected void ButtonBuildTree_Click(object sender, EventArgs e)
        {
            CharacterString = TextBoxString.Text;
            OnBuildTree();
            TextBoxCode.Text = HuffmanCode;
            TextBoxCodeToChar.Text = CodeToChar;
            TextBoxCharToCode.Text = CharToCode;
            LabelStatus.Text = Status;
            TextBoxLog.Text = Log;
            ButtonDecode.Enabled = true;
            ButtonEncode.Enabled = true;
            TextBoxCode.Enabled = true;
        }
    }
}